package com.letv.core.parser;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.letv.core.bean.LetvBaseBean;

/**
 * 移动端基础解析类
 * 
 * @author zhuqiao
 * 
 */
public class LetvMobileParser<T extends LetvBaseBean> extends LetvBaseParser<T, JSONObject> {
    /**
     * 接口信息节点
     */
    protected static final String HEADER = "header";
    /**
     * 接口返回状态：1-正常，2-无数据，3-服务异常
     */
    protected static final String STATUS = "status";
    /**
     * 接口时间轴
     */
    protected static final String MARKID = "markid";
    /**
     * 接口返回数据节点
     */
    public static final String BODY = "body";

    public interface STATE {

        int NORMAL = 1;

        int NODATA = 2;

        int EXCEPTION = 3;

        int NOUPDATE = 4;

        int PARAMETERSERR = 5;

        int IP_DISABLE = 6;// IP被屏蔽
    }

    /**
     * 接口状态
     */
    public int status;

    /**
     * 接口时间轴
     */
    public String markid;

    public LetvMobileParser() {
        this(0);
    }

    public LetvMobileParser(int from) {
        super(from);
    }

    @Override
    protected T parse(JSONObject data) throws Exception {
        if (mClz == null || data == null) {
            return null;
        }

        // 默认用gson解析
        return JSON.parseObject(data.toString(), mClz);
    }

    @Override
    protected boolean canParse(String data) {
        boolean canParse = false;
        try {
            JSONObject object = new JSONObject(data);
            JSONObject headObj = object.optJSONObject(HEADER);
            if (isNull(headObj)) {
                setErrCode(0);
                return false;
            }

            status = headObj.optInt(STATUS);
            setErrCode(status);

            switch (status) {
                case STATE.NORMAL:
                    markid = headObj.optString(MARKID);
                    canParse = true;
                    break;
                case STATE.IP_DISABLE:
                    // IP屏蔽时 必须返回数据解析
                    canParse = true;
                    break;
                case STATE.NOUPDATE:
                    // 无数据更新时,不用解析
                    markid = headObj.optString(MARKID);
                    canParse = false;
                    break;
                default:
                    break;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return canParse;
    }

    @Override
    protected JSONObject getData(String data) throws Exception {
        if (status == STATE.NORMAL || status == STATE.IP_DISABLE) {
            JSONObject object = new JSONObject(data);

            if (isNull(object)) {
                return null;
            }

            JSONObject bodyJson = object.optJSONObject(BODY);
            if (isNull(bodyJson) || TextUtils.equals(bodyJson.toString(), "{}")) {
                return null;
            }

            return bodyJson;
        }

        return null;
    }

    @Override
    public boolean hasUpdate() {
        return status != STATE.NOUPDATE;
    }

    public String getMarkId() {
        return markid;
    }

    public boolean isNewData() {
        return status == STATE.NORMAL;
    }

    public boolean isNoUpdate() {
        return status == STATE.NOUPDATE;
    }

}
